package com.example.hyxydoctor.base;


import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;
import androidx.multidex.MultiDexApplication;

import com.example.hyxydoctor.MainActivity;
import com.facebook.stetho.Stetho;
import com.facebook.stetho.okhttp3.StethoInterceptor;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheEntity;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.cookie.CookieJarImpl;
import com.lzy.okgo.cookie.store.SPCookieStore;
import com.zxy.recovery.core.Recovery;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

public class MyApplication extends MultiDexApplication implements LifecycleOwner {
    public static String session;
    public static String account;
    public static String token;
    public static MyApplication instance;
    private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);
    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        initOkGoHttp(App.INSTANCE);
        mLifecycleRegistry = new LifecycleRegistry(this);
        mLifecycleRegistry.markState(Lifecycle.State.RESUMED);

//        ToastUtils.initStyle(new ToastQQStyle());
//        ToastUtils.init(this);
        Recovery.getInstance()
                .debug(true)
                .recoverInBackground(false)
                .recoverStack(true)
                .mainPage(MainActivity.class)
                .recoverEnabled(true)
                .callback(null)
                .silent(false, Recovery.SilentMode.RECOVER_ACTIVITY_STACK)
                .init(this);

        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {

            }
        });
    }

    public static String getSession() {
        if (session == null) {
            session = (String) SPUtils.get(instance, "session", "");
        }
        return session;
    }
    public static String getToken() {
        if (token == null) {
            token = (String) SPUtils.get(instance, "token", "");
        }
        return token;
    }
    public static String getAccount() {
        if (account == null) {
            account = (String) SPUtils.get(instance, "account", "");
        }
        return account;
    }

    @NonNull
    @Override
    public Lifecycle getLifecycle() {
        return mLifecycleRegistry;
    }

    private void initOkGoHttp(Context context) {
        Stetho.initializeWithDefaults(context);
        CookieJarImpl cookieJar = new CookieJarImpl(new SPCookieStore(context));
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.addNetworkInterceptor(new StethoInterceptor())
                .cookieJar(cookieJar)
                .connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60,TimeUnit.SECONDS)
                .writeTimeout(60,TimeUnit.SECONDS)
//                .addInterceptor(new CookieInterceptor(cookieJar))
        ;
        OkGo.getInstance().init(App.INSTANCE)//必须调用初始化
                .setOkHttpClient(builder.build())//必须设置OkHttpClient
                .setCacheMode(CacheMode.NO_CACHE)               //全局统一缓存模式，默认不使用缓存，可以不传
                .setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)   //全局统一缓存时间，默认永不过期，可以不传
                .setRetryCount(0);
    }
}
